﻿using ProtoBuf.Message;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
using Yunchang.NetworkLayer;

/// <summary>
/// From：张帅
/// PS：自走棋选船界面
/// </summary> 
namespace UI
{
    public class ChessChooseShipPage : OPPage
    {
        public ChessChooseShipItem pref;
        public Transform root;
        int selectShipId;
        Dictionary<int, ChessChooseShipItem> pool = new Dictionary<int, ChessChooseShipItem>();
        protected override void DoOpen()
        {
            base.DoOpen();
            Register();
            selectShipId = AutoChess.AutoChessManager.Ins.cacheData.DefaultShipId;
            Init();
        }

        protected override void DoClose(bool immediately)
        {
            base.DoClose(immediately);
            UnRegister();

        }

        private void Register()
        {
            NetworkDriver.Instance.RegistSocketHandler(OpDefine.SCAutoChessChooseShip, SCAutoChessChooseShip);
        }

        private void UnRegister()
        {
            NetworkDriver.Instance.UnregisterSocketHandler(OpDefine.SCAutoChessChooseShip, SCAutoChessChooseShip);
        }

        private void Init()
        {
            UpdateList();
        }

        void SCAutoChessChooseShip(object obj)
        {
            var msg = obj as SCAutoChessChooseShip;
            if(msg != null)
            {
                selectShipId = msg.shipId;
                AutoChess.AutoChessManager.Ins.cacheData.DefaultShipId = selectShipId;
                foreach (var item in pool)
                {
                    item.Value.SetSelectState(false);
                }

                if (pool.ContainsKey(selectShipId))
                {
                    pool[selectShipId].SetSelectState(true);
                }
            }
        }

        void UpdateList()
        {
            var ships = StaticData.playerData.role.GetShipFleets();
            if (selectShipId == 0)
            {
                selectShipId = Table.DataModel.TableConfig.autochessDefaultShip;
                var haveShips = StaticData.playerData.role.GetHaveShipFleets();
                ShipData defaultShip = null;
                for(int i = 0;i< ships.Count; i++)
                {
                    if(ships[i].shipId == selectShipId)
                    {
                        defaultShip = ships[i];
                        break;
                    }
                }
                if (ships.Contains(defaultShip))
                {
                    ships.Remove(defaultShip);
                    int index = haveShips.Count < ships.Count ? haveShips.Count : ships.Count;
                    ships.Insert(index, defaultShip);
                }
            }
            pref.gameObject.SetActiveEx(true);
            for(int i = 0; i < ships.Count; i++)
            {
                bool isSelect = selectShipId == ships[i].shipId;
                var item = Instantiate(pref);
                item.transform.SetParent(root);
                item.transform.localScale = Vector3.one;
                item.SetItem(ships[i], isSelect, OnShipSelect);
                pool[ships[i].shipId] = item;
            }
            pref.gameObject.SetActiveEx(false);
        }

        void OnShipSelect(int id)
        {
            NetworkDriver.Instance.SendSocketBlock(OpDefine.CSAutoChessChooseShip, new CSAutoChessChooseShip() {
                shipId = id
            }, OpDefine.SCAutoChessChooseShip);
        }
    }
}
